<template>
  <el-dialog
    :title="!dataForm.gradeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="等级名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="等级名称"></el-input>
    </el-form-item>
    <el-form-item label="等级经验" prop="experience">
      <el-input v-model="dataForm.experience" placeholder="等级经验"></el-input>
    </el-form-item>
    <el-form-item label="奖励比例" prop="ratio">
      <el-input v-model="dataForm.ratio" placeholder="奖励比例"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gradeId: 0,
          name: '',
          experience: '',
          ratio: '',
          createTime: '',
          status: ''
        },
        dataRule: {
          name: [
            { required: true, message: '等级名称不能为空', trigger: 'blur' }
          ],
          experience: [
            { required: true, message: '等级经验不能为空', trigger: 'blur' }
          ],
          ratio: [
            { required: true, message: '奖励比例不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gradeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gradeId) {
            this.$http({
              url: this.$http.adornUrl(`/leader/grade/info/${this.dataForm.gradeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.grade.name
                this.dataForm.experience = data.grade.experience
                this.dataForm.ratio = data.grade.ratio
                this.dataForm.createTime = data.grade.createTime
                this.dataForm.status = data.grade.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/leader/grade/${!this.dataForm.gradeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gradeId': this.dataForm.gradeId || undefined,
                'name': this.dataForm.name,
                'experience': this.dataForm.experience,
                'ratio': this.dataForm.ratio,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
